package com.hadluo.qq.client.ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * QQ管理端主界面
 * @author hadluo
 */
public class AdminMainFrame extends JFrame {
    private JList<String> onlineUserList;
    private DefaultListModel<String> userListModel;
    private JTextArea logArea;
    private JButton kickButton;
    private JButton banButton;
    private JButton unbanButton;
    private JButton broadcastButton;

    public AdminMainFrame() {
        initializeUI();
        initializeWebSocket();
    }

    private void initializeUI() {
        setTitle("QQ管理端");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBackground(new Color(35, 35, 35));
        mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));

        // 创建左侧在线用户列表面板
        JPanel leftPanel = createUserListPanel();
        leftPanel.setPreferredSize(new Dimension(200, 0));

        // 创建右侧功能区
        JPanel rightPanel = createFunctionPanel();

        // 添加分隔面板
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
        splitPane.setDividerLocation(200);
        splitPane.setDividerSize(4);
        splitPane.setBorder(null);

        mainPanel.add(splitPane, BorderLayout.CENTER);
        setContentPane(mainPanel);
    }

    private JPanel createUserListPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(new Color(35, 35, 35));

        // 创建标题标签
        JLabel titleLabel = new JLabel("在线用户", SwingConstants.CENTER);
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setBorder(new EmptyBorder(5, 5, 5, 5));

        // 创建用户列表
        userListModel = new DefaultListModel<>();
        onlineUserList = new JList<>(userListModel);
        onlineUserList.setBackground(new Color(45, 45, 45));
        onlineUserList.setForeground(Color.WHITE);
        onlineUserList.setSelectionBackground(new Color(60, 60, 60));
        onlineUserList.setSelectionForeground(Color.WHITE);
        onlineUserList.setBorder(null);

        JScrollPane scrollPane = new JScrollPane(onlineUserList);
        scrollPane.setBorder(null);
        scrollPane.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        panel.add(titleLabel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createFunctionPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(new Color(35, 35, 35));

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        buttonPanel.setBackground(new Color(35, 35, 35));

        kickButton = createStyledButton("踢出用户");
        banButton = createStyledButton("禁言用户");
        unbanButton = createStyledButton("解除禁言");
        broadcastButton = createStyledButton("发送广播");

        buttonPanel.add(kickButton);
        buttonPanel.add(banButton);
        buttonPanel.add(unbanButton);
        buttonPanel.add(broadcastButton);

        // 创建日志区域
        logArea = new JTextArea();
        logArea.setEditable(false);
        logArea.setBackground(new Color(45, 45, 45));
        logArea.setForeground(Color.WHITE);
        logArea.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));

        JScrollPane logScrollPane = new JScrollPane(logArea);
        logScrollPane.setBorder(null);
        logScrollPane.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        // 添加事件监听器
        kickButton.addActionListener(e -> handleKickUser());
        banButton.addActionListener(e -> handleBanUser());
        unbanButton.addActionListener(e -> handleUnbanUser());
        broadcastButton.addActionListener(e -> handleBroadcast());

        panel.add(buttonPanel, BorderLayout.NORTH);
        panel.add(logScrollPane, BorderLayout.CENTER);

        return panel;
    }

    private JButton createStyledButton(String text) {
        JButton button = new JButton(text);
        button.setBackground(new Color(45, 45, 45));
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(60, 60, 60)),
            BorderFactory.createEmptyBorder(5, 15, 5, 15)
        ));
        return button;
    }

    private void initializeWebSocket() {
        // TODO: 实现WebSocket连接
        // 连接成功后开始接收在线用户信息
        // 模拟一些测试数据
        updateOnlineUsers(Arrays.asList("用户1", "用户2", "用户3"));
        appendLog("系统启动完成");
    }

    private void updateOnlineUsers(List<String> users) {
        SwingUtilities.invokeLater(() -> {
            userListModel.clear();
            users.forEach(userListModel::addElement);
        });
    }

    private void appendLog(String message) {
        SwingUtilities.invokeLater(() -> {
            logArea.append(String.format("[%tF %<tT] %s\n", System.currentTimeMillis(), message));
            logArea.setCaretPosition(logArea.getDocument().getLength());
        });
    }

    private void handleKickUser() {
        String selectedUser = onlineUserList.getSelectedValue();
        if (selectedUser == null) {
            JOptionPane.showMessageDialog(this, "请选择要踢出的用户", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // TODO: 实现踢出用户的逻辑
        appendLog(String.format("已踢出用户: %s", selectedUser));
    }

    private void handleBanUser() {
        String selectedUser = onlineUserList.getSelectedValue();
        if (selectedUser == null) {
            JOptionPane.showMessageDialog(this, "请选择要禁言的用户", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // TODO: 实现禁言用户的逻辑
        appendLog(String.format("已禁言用户: %s", selectedUser));
    }

    private void handleUnbanUser() {
        String selectedUser = onlineUserList.getSelectedValue();
        if (selectedUser == null) {
            JOptionPane.showMessageDialog(this, "请选择要解除禁言的用户", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // TODO: 实现解除禁言的逻辑
        appendLog(String.format("已解除用户禁言: %s", selectedUser));
    }

    private void handleBroadcast() {
        String message = JOptionPane.showInputDialog(this, "请输入广播内容:", "发送广播", JOptionPane.PLAIN_MESSAGE);
        if (message != null && !message.trim().isEmpty()) {
            // TODO: 实现发送广播的逻辑
            appendLog(String.format("已发送广播: %s", message));
        }
    }

    // 自定义滚动条UI
    private static class CustomScrollBarUI extends javax.swing.plaf.basic.BasicScrollBarUI {
        @Override
        protected JButton createDecreaseButton(int orientation) {
            return createZeroButton();
        }

        @Override
        protected JButton createIncreaseButton(int orientation) {
            return createZeroButton();
        }

        private JButton createZeroButton() {
            JButton button = new JButton();
            button.setPreferredSize(new Dimension(0, 0));
            return button;
        }

        @Override
        protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
            if (thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
                return;
            }

            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(new Color(100, 100, 100));
            g2.fillRoundRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height, 8, 8);
            g2.dispose();
        }

        @Override
        protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setColor(new Color(45, 45, 45));
            g2.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
            g2.dispose();
        }
    }
}